package com.mouliu.base.concurrent17;


import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * AbortPolicy
 *
 * @author mouliu
 * @create 2018-04-15-上午12:52
 */
public class AbortPolicyDemo {
    public static void main(String[] args){
        // 创建线程池。线程池的"最大池大小"和"核心池大小"都为1，"线程池"的阻塞队列容量为1。
        ThreadPoolExecutor poolExecutor=
                new ThreadPoolExecutor(1,1,0, TimeUnit.SECONDS,new ArrayBlockingQueue<>(1));

        //设置线程池的拒绝策略为AbortPolicy
        poolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());

       try{
           for (int i = 0; i<10; i++){
               Runnable myTask = new MyTask(i,"zhangsan");
               poolExecutor.execute(myTask);
           }
       }catch (RejectedExecutionException e){
           e.printStackTrace();
           // 关闭线程池
           poolExecutor.shutdown();
       }
    }
}
